package com.miaoshare.app.mainActivityTest;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.model.CompleteMultipartUploadResult;
import com.alibaba.sdk.android.oss.model.MultipartUploadRequest;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.utils.ToastUtils;
import com.miaoshare.app.R;
import com.miaoshare.core.base.BaseActivity;
import com.miaoshare.core.base.BasePresenter;
import com.miaoshare.core.utils.DimenUtil;
import com.miaoshare.core.utils.LogUtil;
import com.miaoshare.core.utils.imageLoad.ImageManager;
import com.miaoshare.core.utils.oss.UploadFileUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import demo.ywd.com.listenerlibrary.core.NetType;
import demo.ywd.com.listenerlibrary.core.Network;

/**
 * Created by zxs
 * on 2022/2/10
 * 图片加载测试
 */
public class MainActivityLoadImg extends BaseActivity {
    @BindView(R.id.img_load)
    ImageView imgLoad;
    String url = "http://cn.bing.com/az/hprichbg/rb/Dongdaemun_ZH-CN10736487148_1920x1080.jpg";
    @BindView(R.id.img_load1)
    ImageView imgLoad1;
    @BindView(R.id.img_load2)
    ImageView imgLoad2;
    @BindView(R.id.tv_select_pic)
    ImageView tvSelectPic;

    // 记录进度条数量
    private int progress;
    // 上传中
    private static final int DOWNLOAD = 1;
    // 上传结束
    private static final int DOWNLOAD_FINISH = 2;
    // 上传失败
    private static final int ERROR_DOWN = 3;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main_loadimg;
    }

    @Override
    protected void initView() {
        ImageManager.get().loadCircleImage(url, imgLoad);
//        图片高斯模糊
        ImageManager.get().loadCircleBigmapBlurImage(url, imgLoad1, 50, 20);
//        将文字绘制成图片
        Bitmap bitmap = ImageManager.get().drawImg(Color.parseColor("#0096F6"), Color.parseColor("#0096F6"), Color.WHITE, "击杀解控", DimenUtil.dp2px(this, 35), DimenUtil.dp2px(this, 35));
        imgLoad2.setImageBitmap(bitmap);
//        图片预览
        imgLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<LocalMedia> imgList = new ArrayList<LocalMedia>();
                List<String> img_url = new ArrayList<>();
                img_url.add(url);
                for (String url : img_url) {
                    LocalMedia loc = new LocalMedia();
                    loc.setPath(url);
                    imgList.add(loc);
                }
                ImageManager.get().previewImage(MainActivityLoadImg.this, 0, imgList);
//                startActivity(new Intent(MainActivityLoadImg.this, BaseWebActivity.class)
//                        .putExtra("URL_CONTENT","url"));
            }
        });

    }

    @OnClick(R.id.tv_select_pic)
    public void onClick() {
        selectImageGallery(9);
//        selectVideoGallery(9);
//        selectVideoCamera();
    }

    @Override
    protected ArrayList<LocalMedia> analyticalSelectResults(ArrayList result) {
        ArrayList<LocalMedia> localMediaArrayList = super.analyticalSelectResults(result);
//        本地图片显示
        ImageManager.get().loadLocalImage(localMediaArrayList.get(0).getRealPath(), tvSelectPic, 4);
//        上传本地图片到 阿里云或直接上传服务器
//        List<String> path = new ArrayList<>();
//        for (int i = 0; i < localMediaArrayList.size(); i++) {
//            path.add((localMediaArrayList.get(0)).getRealPath());
//        }
//        uploadVideo(path);
        LogUtil.d(this.getClass().getName() + "--------" + (localMediaArrayList.get(0)).getRealPath());
        return super.analyticalSelectResults(result);
    }


    @Override
    protected void loadData() {

    }

    @Nullable
    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    //监听网络变化
    @Network(netType = NetType.AUTO)
    public void onNetChanged(NetType netType) {
        Log.e("网络状态", netType.toString());
        ToastUtils.showToast(this, "网络状态：" + netType);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 正在上传
                case DOWNLOAD:
                    // 设置进度条位置

                    break;
                case DOWNLOAD_FINISH:
                    // 上传文件完成
                    break;
                case ERROR_DOWN:
                    // 失败
                    break;
                default:
                    break;
            }
        }
    };

    //上传文件
    private void uploadVideo(List<String> pathList) {
        if (pathList.size() != 0) {
            //  showProgressDialog();
            UploadFileUtil.get().uploadFileMultipart(pathList.get(0), new UploadFileUtil.UploadFileCallback() {
                @Override
                public void onProgress(MultipartUploadRequest request, long currentSize, long totalSize) {
                    // 计算进度条位置
                    progress = (int) (((float) currentSize / totalSize) * 100);
                    mHandler.sendEmptyMessage(DOWNLOAD);
                }

                @Override
                public void onSuccess(MultipartUploadRequest request, CompleteMultipartUploadResult result) {
                    // 得到地址可自行上传服务器
                    ImageManager.get().loadCircleImage(result.getLocation(), imgLoad);
                    mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
//                    dismissProgressDialog();
                }

                @Override
                public void onFailure(MultipartUploadRequest request, ClientException
                        clientException, ServiceException serviceException) {
//                    dismissProgressDialog();
                    mHandler.sendEmptyMessage(ERROR_DOWN);
//                    Toaster.show("上传失败，请稍后重试");
                }
            });
        }
    }

}
